package com.easygem.controller.system;

import com.easygem.common.annotation.ControllerEndpoint;
import com.easygem.common.error.SystemException;
import com.easygem.common.model.system.DeptClient;
import com.easygem.common.response.ResponseBean;
import com.easygem.common.vo.system.DepartmentVO;
import com.easygem.common.vo.system.DeptNodeVO;
import com.easygem.common.vo.system.UserVO;
import com.easygem.system.service.EmployeeService;
import com.easygem.system.service.UserService;
import com.easygem.system.service.impl.DeparmentServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 文晓平
 * @Date 2023/12/25 11:51
 * @Version 1.0
 **/
@Api(tags = "系统模块-部门相关接口")
@RequestMapping("/system/dept")
@RestController
public class DeptController {

    @Autowired
    private DeparmentServiceImpl deptService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private UserService userService;

    /**
     * 加载部门树
     *
     * @return
     */
    @ApiOperation(value = "加载部门树", notes = "获取所有部门树，以及展开项")
    @GetMapping("/tree")
    public ResponseBean<Map<String, Object>> tree() {
        List<DeptNodeVO> menuTree = deptService.findDeptTree();
        List<Long> ids = deptService.findOpenIds();
        Map<String, Object> map = new HashMap<>();
        map.put("tree", menuTree);
        map.put("open", ids);
        return ResponseBean.success(map);
    }

    /**
     * 加载部门信息，根据当前登录用户获得当前部门及所有下级部门
     * @return
     */
    @ApiOperation(value = "加载部门信息", notes = "根据当前登录用户获得当前部门及所有下级部门")
    @GetMapping("/findDeptByAccountName/{accountName}")
    public ResponseBean<Map<String, Object>> findDeptByAccountName(@PathVariable(value = "accountName",required = false) String accountName) {
        Map<String, Object> map = new HashMap<>();
        if (accountName!=null||!accountName.equals("")){
            map.put("tree", deptService.findDeptByAccountName(accountName));
        }else {
            map.put("tree", deptService.findDeptTree());
        }
        return ResponseBean.success(map);
    }

    /**
     * 加载部门信息，根据当前登录用户获得当前部门及所有下级部门
     * @return
     */
    @ApiOperation(value = "加载部门信息", notes = "根据当前登录用户获得当前部门及所有下级部门")
    @GetMapping("/findDept")
    public ResponseBean<List<DeptClient>> findDept() {
        List<DeptClient> menuTree = deptService.findDeptTreeByDeptId();
        return ResponseBean.success(menuTree);
    }

    /**
     * 新增部门/按钮
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "新增菜单/按钮失败", operation = "新增菜单/按钮")
    @ApiOperation(value = "新增菜单")
    @RequiresPermissions({"clientDept:add"})
    @PostMapping("/add")
    public ResponseBean<Map<String, Object>> add(@RequestBody @Validated DepartmentVO departmentVO) {
        DeptClient node = deptService.add(departmentVO);
        Map<String, Object> map = new HashMap<>();
        map.put("id", node.getDeptId());
        map.put("deptName", node.getDeptName());
        map.put("children", new ArrayList<>());
        return ResponseBean.success(map);
    }

    /**
     * 删除部门/按钮
     *
     * @param id
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除部门失败", operation = "删除部门")
    @ApiOperation(value = "删除部门", notes = "根据id删除部门节点")
    @RequiresPermissions({"clientDept:delete"})
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id) throws SystemException {
        deptService.delete(id);
        return ResponseBean.success();
    }
    /**
     * 删除部门/按钮
     *
     * @param id
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除部门失败", operation = "删除部门")
    @ApiOperation(value = "删除部门", notes = "根据id删除部门节点")
    @RequiresPermissions({"clientDept:allDelete"})
    @DeleteMapping("/allDelete/{id}")
    public ResponseBean allDelete(@PathVariable Long id) throws SystemException {
        deptService.allDelete(id);
        return ResponseBean.success();
    }

    /**
     * 删除部门/按钮
     *
     * @param id
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除部门失败", operation = "删除部门")
    @ApiOperation(value = "删除部门", notes = "根据id删除部门节点")
    @RequiresPermissions({"clientDept:allUpdate"})
    @DeleteMapping("/allUpdate/{id}")
    public ResponseBean allUpdate(@PathVariable Long id) throws SystemException {
        deptService.allUpdate(id);
        return ResponseBean.success();
    }

    /**
     * 部门详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "部门详情", notes = "根据id编辑部门，获取部门详情")
    @RequiresPermissions({"clientDept:edit"})
    @GetMapping("/edit/{id}")
    public ResponseBean<DepartmentVO> edit(@PathVariable Long id) throws SystemException {
        DepartmentVO departmentVO = deptService.edit(id);
        return ResponseBean.success(departmentVO);
    }

    /**
     * 根据部门编号查询部分所有人
     * @param deptId
     * @return
     */
    @ApiOperation(value = "用户列表", notes = "根据部门编号得到用户列表")
    @RequiresPermissions({"clientDept:viewList"})
    @GetMapping("/getUser/{deptId}")
    public ResponseBean<List<UserVO>> getUsers(@PathVariable("deptId") Long deptId) throws SystemException {
        List<UserVO> list = userService.findUserListByDept(deptId);
        return ResponseBean.success(list);
    }

    /**
     * 更新部门
     *
     */
    @ControllerEndpoint(exceptionMessage = "更部门失败", operation = "更新部门")
    @ApiOperation(value = "更新部门", notes = "根据id更新部门节点")
    @RequiresPermissions({"clientDept:update"})
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id, @RequestBody @Validated DepartmentVO departmentVO) throws SystemException {
        deptService.update(id, departmentVO);
        return ResponseBean.success();
    }


}
